<?php

class Core_Data_SQL_Structure_Table {

    private $arrFields = null;

    public $Database;
    private $strName;

    function __construct ( $objDatabase, $strName ) {
        $this->Database = $objDatabase;
        $this->strName = $strName;
    }

    function __toString () {
        return $this->strName;
    }

    public function rename ( $strName ) {
        $strQuery = "RENAME TABLE `" . $this . "` TO `" . $strName . "`";
        $this->Database->Connection->query( $strQuery );
        $this->strName = $strName;
    }

    public function fields() {

        if ( $this->arrFields === null ) {

            $strQuery = "SHOW FIELDS FROM `" . $this . "";
            $arrResult = $this->Database->Connection->fetch( $strQuery );

            foreach ( $arrResult as $arrField ) {
                
                $strName = $arrField["Field"];
                $constDataType = Core_Data_SQL_DataTypes::getFromResultText( $arrField["Type"], $this->Database->Connection->ServerInformation->Type, $intLength );

                $objField = new Core_Data_SQL_Structure_Field ( $strName, $constDataType );
                $objField->setLength( $intLength );

                if ( $arrField["Null"] == "NO" ) {
                    $objField->setRequired();
                }

                if ( $arrField["Key"] ) {
                    $objField->setKeyType( Core_Data_SQL_KeyTypes::getFromResultText( $arrField["Key"], $this->Database->Connection->ServerInformation->Type ) );
                }

                if ( $arrField["Extra"] == "auto_increment" ) {
                    $objField->autoIncrement( true );
                }


                $this->arrFields[] = $objField;
            }

        }

        return (object) $this->arrFields;

    }

}

?>